package com.pd.kit;

import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;

/**
 * Created by dong peng
 * DATE : 2016/12/18
 * TIME : 22:57.
 */


public class JsonKit {

    private static ObjectMapper objectMapper = null;

    private JsonKit(){}

    static {
        objectMapper = new ObjectMapper();
    }

    public static String toJson(Object object) {
        try {
            return objectMapper.writeValueAsString(object);
        } catch (Exception e) {
            throw e instanceof RuntimeException ? (RuntimeException) e : new RuntimeException(e);
        }
    }

    /**
     * Json串转为Object
     * @param jsonString
     * @param type
     * @param <T>
     * @return
     */
    public static <T> T parse(String jsonString, Class<T> type) throws IOException {
            return objectMapper.readValue(jsonString, type);
    }

}
